package com.seaengine.util
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;

	/**
	 * 获得SWF中的域
	 * @author 曹鹏
	 */	
	public class ApplicationDomain
	{
		public function ApplicationDomain()
		{
			
		}
		
		/**
		 * @param loaderInfo 加载它的LoaderInfo对象
		 * @param name	此显示对象在域中的名称
		 */		
		public static function getDomain(loaderInfo:LoaderInfo,name:String):DisplayObject
		{
			var cla:Class;
			var dis:DisplayObject;
			try
			{
				cla = loaderInfo.applicationDomain.getDefinition(name) as Class;
				if(cla != null)//获得资源时执行
				{
					try//非绑定BitMap
					{
						dis = DisplayObject(new cla());
					}
					catch(e:Error)//绑定BitMap
					{
						var bitmapData:BitmapData = BitmapData(new cla(0,0));
						var bitmap:Bitmap = new Bitmap();
						bitmap.bitmapData = bitmapData;
						bitmapData.draw(bitmap);
						dis = bitmap;
						bitmapData = null;
					}
				}
			}
			catch(e:Error)//没有得到任何资源时执行
			{
				var sprite:Sprite = new Sprite();
				sprite.graphics.beginFill(0x00ff00);
				sprite.graphics.drawRoundRect(0,0,50,50,10);
				sprite.graphics.endFill();
				dis = sprite;
			}
			return dis;
		}
	}
}